<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title>排班组合</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../common/mui/mui.css" rel="stylesheet" />
		<link href="../../common/css/yahei.css" rel="stylesheet" />
		<link href="../../common/css/iconfont.css" rel="stylesheet" />
		<link href="../../common/css/app.css" rel="stylesheet" />
		<style>
			.areaTip{
				width: 100%;
				height: var(--size40);
				line-height: var(--size20);
				padding: var(--size15);
				padding-bottom: var(--size5);
				background-color: #F1F0F5;
				font-size: var(--size14);
				color: #999999;
			}
			
			input[type='text']{
				margin: 0;
				border: none;
				height: var(--size45);
				font-size: var(--size14);
			}
			.itemArea{
				height: 4.2rem; /* 105px */
				width: 100%;
				background-color: white;
			}
			#wrapper{
				top: calc(44px + 10.8rem); /* 40+45+40+105 + 40=270 */
				bottom: var(--size60);
				background-color: white;
			}
			table{
				table-layout: fixed;
				width: 100%;
				border-collapse: separate;
				border-spacing: var(--size15) var(--size20);
			}
			td{
				height: calc(var(--size25) + 2px);
				border: solid 1px #DDDDDD;
				text-align: center;
				font-size: var(--size12);
			}
			#itemBody td{
				position: relative;
			}
			#itemBody td div{
				position: absolute;
				top: -0.36rem;
				right: -0.32rem;
				width: var(--size18);
				height: var(--size18);
				color: red;
			}
			.bottomArea{
				position: absolute;
				left: 0;
				bottom: 0;
				width: 100%;
				height: var(--size50);
				display: flex;
				font-size: var(--size16)
			}
			.deleteBtn,.saveBtn{
				width: 50%;
				height: 100%;
				line-height: var(--size50);
				text-align: center;
				color: white;
				background-color: var(--mainColor);
			}
			.deleteBtn{
				color: var(--mainColor);
				background-color: white;
			}
			.new .deleteBtn{
				display: none;
			}
			.new .saveBtn{
				width: 100%;
			}
		</style>
	</head>

	<body class="YaHei">
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">编辑组合</h1>
		</header>
		<div class="mui-content">
			<div class="areaTip">组合名称</div>
			<input id="groupName" type="text" class="mui-input-clear" placeholder="请输入组合名称" />
			<div class="areaTip">组合班次</div>
			<div class="itemArea">
				<table>
					<colgroup><col width="20%"><col width="20%"><col width="20%"><col width="20%"><col width="20%">
					</colgroup>
					<tbody id="itemBody"></tbody>
				</table>
			</div>
			<div class="areaTip">点击下方班次添加组合</div>
			<div id="wrapper" class="mui-scroll-wrapper">
				<div class="mui-scroll mui-scroll-phone">
					<table>
						<colgroup><col width="20%"><col width="20%"><col width="20%"><col width="20%"><col width="20%">
						</colgroup>
						<tbody id="tbody"></tbody>
					</table>
				</div>
			</div>
			<div class="bottomArea">
				<div id="deleteBtn" class="deleteBtn">删除</div>
				<div id="saveBtn" class="saveBtn">保存</div>
			</div>
		</div>
		<script src="../../common/mui/mui.js"></script>
		<script src="../../common/js/jquery-3.4.1.min.js"></script>
		<script src="../../common/js/globalSet.js"></script>
		<script src="../../common/js/DHCWebService.js"></script>
		<script type="text/javascript">
	var wardID = '',nurseid='',rw="",pageType="";
	mui.init();
	mui.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getGroupData(){
		var pdict = {"rw":rw} 
		var paramDict = {'method':'web.INMDBComm:GetWardPostGroup','params':JSON.stringify(pdict),'type':'RecMethod'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1' || result['data'].length==0) {
				mui.toast(result['msg'])
				$('#tbody')[0].innerHTML = ''
				return
			}
			var dataDict = result['data']
			updateGroupUI(dataDict)
		}, function(errorStr) {
			$('#itemBody')[0].innerHTML = ''
		});
	}
	
	function updateGroupUI(dataDict){
		mui('#groupName')[0].value=dataDict['GroupDesc']
		
		var postArr = dataDict['GroupPostDesc'].split(',')
		var postIdArr = dataDict['GroupPost'].split(',')
		
		var html = '<tr>'
		for (var i = 0; i < postIdArr.length; i++) {
			if (i != 0 &&i % 5 == 0) {
				html += '</tr><tr>'
			}
			html +='<td data-id="'+postIdArr[i]+'">'+postArr[i]+'<div class="nurfont nur-error"></div></td>'
		}
		html += '</tr>'
		$('#itemBody')[0].innerHTML = html
		mui('#itemBody td').each(function (index,td) {
			td.addEventListener('tap',function(){
				removeOneItem(this)
			})
		})
	}
	
	function saveGroupData(){
		
		var groupName=mui('#groupName')[0].value;
		if (groupName == '') {
			mui.toast('组合名称不能为空')
			return;
		}
		
		
		var schIds = ''
		mui('#itemBody td').each(function (index,element) {
			if (schIds != '') {
				schIds +=','
			}
			schIds += element.getAttribute('data-id')
		})
		if (schIds == '') {
			mui.toast('请选择要组合的班次')
			return
		}
		//parr GroupDesc|组合描述^GroupPost|组合病区班次Id^rw|组合Id^GroupWard|病区Id
		var pdict = {'parr':'GroupDesc|'+groupName+'^GroupPost|'+schIds+'^rw|'+rw+'^GroupWard|'+wardID} 
		var paramDict = {'method':'web.INMDBComm:SaveWardPostGroup','params':JSON.stringify(pdict),'type':'Method'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if(isNaN(msg)) {
				mui.toast(result['msg'])
				return
			}
			var pageId = 'nurSchZuHe'
			if (pageType=="loc") {
				pageId = 'nurSchLoc'
			}
			var zuhePage = plus.webview.getWebviewById(pageId)
			if (zuhePage) {
				zuhePage.evalJS('refreshPageData()')
			}
			mui.back()
		}, function(errorStr) {
			mui.toast(errorStr)
			return
		});
	}
	function getPostData(){
		var type="" //P:排班 H:休假 S:特殊 空表是全部
		var pdict = {"start":0,'limit':"",'parr':'^'+wardID+'^'+type} //
		var paramDict = {'method':'web.INMDBComm:FindWardPostList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1' || result['data'].length==0) {
				mui.toast(result['msg'])
				$('#tbody')[0].innerHTML = ''
				return
			}
			var dataArr = result['data']
			var html = '<tr>'
			for (var i = 0; i < dataArr.length; i++) {
				var oneData = dataArr[i]
				if (i != 0 &&i % 5 == 0) {
					html += '</tr><tr>'
				}
				html +='<td data-id="'+oneData['rw']+'">'+oneData['PostDesc']+'</td>'
			}
			html += '</tr>'
			$('#tbody')[0].innerHTML = html
			mui('#tbody td').each(function (index,td) {
				td.addEventListener('tap',addOneItem)
			})
		}, function(errorStr) {
			$('#tbody')[0].innerHTML = ''
		});
	}
	function addOneItem(){
		var desc = this.innerText
		var itemBody = $('#itemBody')[0]
		var itemCount = itemBody.querySelectorAll('td').length
		if (itemCount >=7) {
			mui.toast('最多只能添加7个班次哦')
			return
		}
		var tr = itemBody.lastElementChild
		if (itemCount % 5 == 0) {
			tr = document.createElement('tr')
			itemBody.appendChild(tr)
		}
		var td = document.createElement('td')
		td.setAttribute('data-id',this.getAttribute('data-id'))
		td.innerHTML = desc +'<div class="nurfont nur-error"></div>'
		td.addEventListener('tap',function(){
			var dom = this
			removeOneItem(dom)
			return
			mui.confirm('确认从组合中删除这个班次吗?','温馨提示',['取消','删除'],function (e) {
				if (e.index == 1) {
					removeOneItem(dom)
				}
			},'div')
		})
		tr.appendChild(td)
	}
	function removeOneItem(dom){
		var tr = dom.parentNode
		tr.removeChild(dom)
		var itemBody = $('#itemBody')[0]
		if (!tr.firstElementChild) {
			itemBody.removeChild(tr)
			return
		}
		var lastTr = itemBody.lastElementChild
		if (tr == lastTr) {
			return
		}
		var firstTd = lastTr.firstElementChild
		lastTr.removeChild(firstTd)
		tr.appendChild(firstTd)
		if (!lastTr.firstElementChild) {
			itemBody.removeChild(lastTr)
		}
	}
	function deleteOneGroup(){
		var pdict = {"rw":rw}
		var paramDict = {'method':'web.INMDBComm:DeleteWardPostGroup','params':JSON.stringify(pdict),'type':'Method'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msg'] != '1') {
				mui.toast('删除失败')
				return
			}
			var zuhePage = plus.webview.getWebviewById('nurSchZuHe')
			if (zuhePage) {
				zuhePage.evalJS('refreshPageData()')
			}
			mui.back()
		}, function(errorStr) {
			mui.toast('删除失败')
		});
	}

	function baseSetup(){
		mui('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		
		mui('#saveBtn')[0].addEventListener('tap',saveGroupData)
		mui('#deleteBtn')[0].addEventListener('tap',function(){
			if (!rw || rw=="") {
				return
			}
			mui.confirm('确定删除这个组合吗','温馨提示',['取消','确认'],function (e) {
				if (e.index == 1) {
					deleteOneGroup()
				}
			})
		})
		var self = plus.webview.currentWebview();
		var groupId = self.rw;
		wardID = self.wardID;
		nurseid=self.nurseid
		pageType = self.type
		getPostData();
		if (groupId) {
			rw=groupId
			getGroupData()
		}
		if (!rw || rw=="") {
			mui('.bottomArea')[0].classList.add('new')
		}
	}
		</script>
	</body> 

</html>

<!-- 
 病区所有班次
 {"rw":"68","PostDesc":"A","WardDR":"29","WardDesc":"内分泌科护理单元","PostDR":"1","PostDesc2":"A",
 "PostType":"P","TypeDesc":"值班","HolidayTime":"","TimeDesc":"","PostClass":"","PostStTime1":"08:00",
 "PostEndTime1":"16:00","PostStTime2":"","PostEndTime2":"","PostStTime3":"","PostEndTime3":"",
 "PostMinutes":"8时0分","PostDayMinutes":"8时0分","PostNextMinutes":"0时0分","PostRemark":"","PostSort":"1",
 "PostCategory":"W","PostCrtDate":"2021-06-07","PostCrtTime":"13:14:24","PostLevel":"一线班","PostFactor":""}
 rw	id
 CrtUserName	创建人姓名
 HolidayTime	休假时间
 PostCategory	班次类型
 PostClass	班次
 PostCrtDate	创建日期
 PostCrtTime	创建时间
 PostCrtUser	创建人user id
 PostDR	全局班次Id
 PostDayMinutes	当日时长
 PostDesc	病区班次描述
 PostDesc2	全局班次描述
 PostEndTime1	一段结束时间
 PostEndTime2	二段结束时间
 PostEndTime3	三段结束时间
 PostFactor	班种系数
 PostLevel	班次级别
 PostMinutes	总时长
 PostNextMinutes	次日时长
 PostRemark	备注
 PostSort	序号
 PostStTime1	一段开始时间
 PostStTime2	二段开始时间
 PostStTime3	三段开始时间
 PostType	排班类型
 TimeDesc	休假时间描述
 TypeDesc	排班类型描述
 WardDR	病区
 WardDesc	病区描述
 

 排班套详情
 CombDesc 组合描述
 CombEndDate 停用日期
 CombPost  组合班次Id
 CombPostDesc  组合班次描述
 CombStDate  启用日期
 CombWard  病区Id
 CombWardDesc  病区描述
 CreateDate  创建日期
 CreateTime   创建时间
 Creator  创建人
 CreatorName  创建人姓名
 rw  id
 -->
